<?
/*
 *  GGB - Gelin's Guest Book.
 *  Copyright (C) 2006  Denis Nelubin aka Gelin
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *  http://gelin.ru
 *  mailto:den@gelin.ru
 *
 *  Description:    Form for adding message.
 *
 *  History:
 *  2006-12-31  Gelin   Initially created.
 *
 */

require_once('MessageForm.php');

/**
 *  Form for adding/modifying message by admin.
 */
class AdminMessageForm extends MessageForm {

    /**
     *  Creates array of form fields.
     *  @return array of form fields.
     */
    function prepareAddForm() {
        global $config;
        $form = parent::prepareForm();
        /*
        $form['name'] = $this->prepareFormField('name', getParameter('name'));
        $form['email'] = $this->prepareFormField('email', getParameter('email'));
        $form['url'] = $this->prepareFormField('url', getParameter('url'));
        $form['message'] = $this->prepareFormField('message', getParameter('message'));
        */
        $form['action'] = '?action=admin-add-submit';
        $form['reply'] = $this->prepareFormField('reply', getParameter('reply'));
        //populate default values
        $form['name']['value'] = $config['admin']['username'];
        $form['email']['value'] = $config['admin']['email'];
        $form['url']['value'] = $_SERVER['HTTP_HOST'];
        return $form;
    }

    /**
     *  Creates array of form fields for edit message form.
     *  Fields are populated by values extracted from database.
     *  @param  $message   message which will be edited.
     *  @return array of form fields.
     */
    function prepareEditForm($message) {
        $form = $this->prepareAddForm();
        //set values from database
        $form['action'] = '?action=admin-edit-submit';
        $form['id'] = $this->prepareFormField('id', $message['id']);
        $form['name']['value'] = $message['name'];
        $form['email']['value'] = $message['email'];
        $form['url']['value'] = $message['url'];
        $form['message']['value'] = $message['message'];
        $form['reply']['value'] = $message['reply'];
        return $form;
    }

    /**
     *  Creates array of form fields for delete message form.
     *  @param  $message   message which will be edited.
     *  @return array of form fields.
     */
    function prepareDeleteForm($message) {
        $form['action'] = '?action=admin-delete-submit';
        $form['id'] = $this->prepareFormField('id', $message['id']);
        return $form;
    }

    /**
     *  Process request and creates new message, submitted from form.
     *  @return message created from form
     */
    function getFromRequest() {
        global $config;
        $message = parent::getFromRequest();
        /*
        $message['remote_ip'] = trim($_SERVER['REMOTE_ADDR']) != '' ? $_SERVER['REMOTE_ADDR'] : NULL;
        $message['forward_ip'] = trim($_ENV['HTTP_X_FORWARDED_FOR']) != '' ? $_ENV['HTTP_X_FORWARDED_FOR'] : NULL;
        $message['post_time'] = time();
        $message['name'] = $this->getFormField('name');
        $message['email'] = $this->getFormField('email');
        $message['url'] = $this->getFormField('url');
        $message['message'] = $this->getFormField('message');
        */
        $message['id'] = $this->getFormField('id');
        $message['reply'] = $this->getFormField('reply');
        if (isset($message['reply'])) {
            $message['reply_time'] = time();
        }
        return $message;
    }

}

?>